function replaceHeader(user_agent, requestHeaders) {
    console.log("1111");
    var newHeaders = [];
    for (var i = 0; i < requestHeaders.length; i++)
    {
        console.log(requestHeaders[i].name);
        if (requestHeaders[i].name == "User-Agent")
            newHeaders.push({name: "User-Agent", value: user_agent});
        else
        {
            newHeaders.push(requestHeaders[i]);
        }
    }

    console.log(newHeaders);
    return newHeaders;
}

function updateListeners() {
    if (!listener) listener = function (details) {
        //console.log(details);
        var header_map = {requestHeaders: details.requestHeaders};
        if (details && details.url && details.requestHeaders && details.requestHeaders.length > 0)
        {
            header_map = {requestHeaders: replaceHeader("Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25", details.requestHeaders)};
        }
        return header_map;
    };
    chrome.webRequest.onBeforeSendHeaders.addListener(listener, {"urls": ["https://*.mgtv.com/*"]}, ["requestHeaders", "blocking", "extraHeaders"]);
}

var listener = null;
updateListeners();
